package org.example.teaView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class HandleSeeAllStudent implements ActionListener {
    SeeStuView seeStuView;

    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread;

    public void setSeeStuView(SeeStuView seeStuView) {
        this.seeStuView = seeStuView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        InetAddress address= null;
        socket=new Socket();
        thread=new Thread(seeStuView);
        try {
            address = InetAddress.getByName("127.0.0.1");
            InetSocketAddress socketAddress=new InetSocketAddress
                    (address,4331);
            socket.connect(socketAddress);
            in =new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            thread.start();
        } catch (IOException ex) {
            socket=new Socket();
        }

        if (e.getSource() == seeStuView.back){
            seeStuView.setVisible(false);
            new Firstview();
        }else if (e.getSource() == seeStuView.seeAll){
            try {
                out.writeUTF("查看学生成绩");
                seeStuView.tableModel.setRowCount(0);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }else if (e.getSource() == seeStuView.search){
            if (seeStuView.input_ID.getText().length() == 0){
                seeStuView.hintMess.setText("请输入学生ID");
                return;
            }
            String ID = seeStuView.input_ID.getText();
            try {
                out.writeUTF("搜索学生ID"+ID);
                seeStuView.tableModel.setRowCount(0);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
